package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetmealMapper setmealMapper;

    //新增套餐
    @Override
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        //数据拷贝
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmeal.setStatus(StatusConstant.DISABLE);
        //先往套餐表插入数据
        setmealMapper.save(setmeal);
        //获取id值
        Long dishId = setmeal.getId();
        //在dto中找flavor的数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(dishId);
            });
            //再往口味表插入数据
            setmealMapper.insertBatch(setmealDishes);
        }


    }

    //套餐分页查询
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.page(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    //删除套餐
    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {

        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            //判断是否为起售状态
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        for (Long id1 : ids) {
            setmealMapper.deleteById(id1);
            setmealMapper.deleteBySetmealId(id1);
        }
    }

    //根据id查询套餐
    @Override
    public SetmealVO selectById(Long id) {
        //先查询套餐
        Setmeal setmeal = setmealMapper.seletSetmeal(id);
        List<SetmealDish> setmealDishes = setmealMapper.getBySetmealId(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    //修改套餐
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //先修改套餐
        setmealMapper.updateSetmeal(setmeal);
        //得到套餐表的id
        Long setmealId = setmealDTO.getId();
        //先删除套餐表里的全部菜品
        setmealMapper.deleteBySetmealId(setmealId);
        //由于菜品有很多，需要遍历
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            //先往中间表插入setmealId
            setmealDish.setSetmealId(setmealId);
        });
        //重新往套餐表里添加菜品
        setmealMapper.insertBatch(setmealDishes);
    }

    @Override
    public void StartOrStop(Long status, Long id) {
        setmealMapper.startOrStop(status,id);
    }
}
